package com.lineage.server.serverpackets;

import com.lineage.config.ConfigOther;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.utils.Random;
import com.lineage.server.world.World;

public class S_AttackPacketPc extends ServerBasePacket {
  private byte[] _byte;
  
  private int _actid;
  
  public S_AttackPacketPc(L1PcInstance pc, L1Character target, int type, int dmg) {
    this._byte = null;
    this._actid = 1;
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(pc); 
    writeC(this._actid);
    writeD(pc.getId());
    int targetobj = target.getId();
    L1Object obj = World.get().findObject(targetobj);
    if (obj instanceof L1NpcInstance) {
      L1NpcInstance npc = (L1NpcInstance)obj;
      int[] bow_GFX_Arrow = ConfigOther.AtkNo;
      int array[], n = (array = bow_GFX_Arrow).length;
      int i = 0;
      while (i < n) {
        int gfx = array[i];
        if (npc.getTempCharGfx() == gfx)
          targetobj = 0; 
        i++;
      } 
      switch (npc.getTempCharGfx()) {
        case 2544:
        case 16778:
          targetobj = 0;
          pc.sendPackets(new S_Sound(1151));
          break;
      } 
    } 
    if (obj instanceof L1PcInstance) {
      L1PcInstance pc2 = (L1PcInstance)obj;
      int[] atkpc = ConfigOther.AtkNo_pc;
      int array[], n = (array = atkpc).length;
      int i = 0;
      while (i < n) {
        int gfx = array[i];
        if (pc2.getTempCharGfx() == gfx)
          targetobj = 0; 
        i++;
      } 
    } 
    writeD(targetobj);
    if (target instanceof L1PcInstance) {
      L1PcInstance tg = (L1PcInstance)target;
      if (tg.getTempCharGfx() == 23353 || tg.getTempCharGfx() == 23759)
        dmg = 0; 
    } 
    if (dmg > 0) {
      writeH(10);
    } else {
      writeH(0);
    } 
    writeC(pc.getHeading());
    writeH(0);
    writeH(0);
    writeC(type);
  }
  
  public S_AttackPacketPc(L1PcInstance pc, L1Character target) {
    this._byte = null;
    this._actid = 1;
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(pc); 
    writeC(this._actid);
    writeD(pc.getId());
    writeD(target.getId());
    writeH(0);
    writeC(pc.getHeading());
    writeH(0);
    writeH(0);
    writeC(0);
  }
  
  public S_AttackPacketPc(L1PcInstance pc) {
    this._byte = null;
    this._actid = 1;
    writeC(30);
    if (Random.nextInt(100) < 25)
      this._actid = SpecialActid(pc); 
    writeC(this._actid);
    writeD(pc.getId());
    writeD(0);
    writeH(0);
    writeC(pc.getHeading());
    writeH(0);
    writeH(0);
    writeC(0);
  }
  
  private int SpecialActid(L1PcInstance pc) {
    int tempgfxid = pc.getTempCharGfx();
    L1ItemInstance weapon = pc.getWeapon();
    if (weapon == null)
      return this._actid; 
    int weapontype = weapon.getItem().getType1();
    if (tempgfxid == 13715 && weapontype == 4)
      this._actid = 73; 
    if (tempgfxid == 13717 && weapontype == 4)
      this._actid = 73; 
    if (tempgfxid == 13719 && weapontype == 50)
      this._actid = 79; 
    if (tempgfxid == 13721 && weapontype == 50)
      this._actid = 79; 
    if (tempgfxid == 13723 && weapontype == 4)
      this._actid = 73; 
    if (tempgfxid == 13725 && weapontype == 4)
      this._actid = 73; 
    if (tempgfxid == 13727 && weapontype == 40)
      this._actid = 77; 
    if (tempgfxid == 13729 && weapontype == 40)
      this._actid = 77; 
    if (tempgfxid == 13731 && weapontype == 54)
      this._actid = 80; 
    if (tempgfxid == 13731 && weapontype == 58)
      this._actid = 81; 
    if (tempgfxid == 13733 && weapontype == 54)
      this._actid = 80; 
    if (tempgfxid == 13733 && weapontype == 58)
      this._actid = 81; 
    if (tempgfxid == 13735 && weapontype == 24)
      this._actid = 41; 
    if (tempgfxid == 13737 && weapontype == 24)
      this._actid = 41; 
    if (tempgfxid == 13741 && weapontype == 40)
      this._actid = 77; 
    if (tempgfxid == 13743 && weapontype == 11)
      this._actid = 93; 
    if (tempgfxid == 13743 && weapontype == 24)
      this._actid = 76; 
    if (tempgfxid == 13745 && weapontype == 11)
      this._actid = 93; 
    if (tempgfxid == 13745 && weapontype == 24)
      this._actid = 76; 
    if (tempgfxid >= 13216 && tempgfxid <= 13220)
      this._actid = 30; 
    if (tempgfxid == 12280)
      this._actid = 30; 
    if (tempgfxid == 12283)
      this._actid = 30; 
    if (tempgfxid == 12286)
      this._actid = 30; 
    if (tempgfxid == 12295)
      this._actid = 30; 
    return this._actid;
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
